package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

public class GzmamaNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;

		String[] regex={ 
							"http://www.gzmama.com/thread-[0-9]+-1-1.html",
							"http://www.gzmama.com/forum.php[?]tid=[0-9]+&mod=viewthread"
							};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
		String html=urlMeta.getHtml();
		if (html == null) {
		}
		String title="";
		String content="";
		Long date=0l;
		int commentNum=0;
		int clickNum=0;
		NewsMeta meta=new NewsMeta();
		meta.setUrl(url);
		meta.setType(0);
		Document doc=Jsoup.parse(html);
		urlMeta=null;
		/*
		 * 解析新闻标题
		 */
		if(doc.select("h1.ts a").size() > 1)
		title=doc.select("h1.ts a").get(1).text();
		/*
		 * 解析时间
		 */
		date=DateUtils.matchDate(doc.select("div.authi").text());
		/*
		 * 解析内容
		 */
		content=HtmlCleaner.getContentHtml(url,doc.select("td.t_f"));
		
		String string=doc.select("span.y").text();
		if(StringUtils.isNotBlank(string)){
			Pattern clickReg=Pattern.compile("浏览: [0-9]*");
			Matcher matcher_click=clickReg.matcher(string);
			if (matcher_click.find()) {
				 String click=matcher_click.group();
				 if(click != null){
					 click=click.replace("浏览: ", "");
					 try {
						clickNum=Integer.parseInt(click);
					} catch (NumberFormatException e) {
						clickNum=0;
					}
				 }
			}
			Pattern commentReg=Pattern.compile("回复: [0-9]*");
			Matcher matcher_comment=commentReg.matcher(string);
			if (matcher_comment.find()) {
				 String comment=matcher_comment.group();
				 if(comment != null){
					 comment=comment.replace("回复: ", "");
					 try {
						commentNum=Integer.parseInt(comment);
					} catch (NumberFormatException e) {
						commentNum=0;
					}
				 }
			}
			meta.setUpdateUrl(url);
		}
		if (StringUtils.isBlank(title)) {
		}
		/**
		 * 初始化、截取 、设置作者信息*******************************************
		 * 
		 * */
		String author=null;				
		author=doc.select(".authi a").text();
		author=StringUtils.substringBefore(author, "只看");
		meta.setAuthor(author);
		/**
		 *                        	 **************************end *************************
		 * 
		 **/
		meta.setTitle(title);
		meta.setContent(content);
		meta.setDate(date);
		meta.setClickNum(clickNum);
		meta.setCommentNum(commentNum);

		return meta;
	}


	
	public NewsMeta Update(NewsMeta meta) {
		if(meta != null){
			String url=meta.getUpdateUrl();
			if(StringUtils.isNotBlank(url)){
				UrlMeta urlMeta=CrawlHTML.responseToURL(url);
				String htmltxt=urlMeta.getHtml();
				Document doc=Jsoup.parse(htmltxt);
				Integer commentNum=0;
				Integer clickNum=0;
				String string=doc.select("span.y").text();
				if(StringUtils.isNotBlank(string)){
					Pattern clickReg=Pattern.compile("浏览: [0-9]*");
					Matcher matcher_click=clickReg.matcher(string);
					if (matcher_click.find()) {
						 String click=matcher_click.group();
						 if(click != null){
							 click=click.replace("浏览: ", "");
							 try {
								clickNum=Integer.parseInt(click);
							} catch (NumberFormatException e) {
								clickNum=meta.getClickNum();
							}
						 }
					}
					Pattern commentReg=Pattern.compile("回复: [0-9]*");
					Matcher matcher_comment=commentReg.matcher(string);
					if (matcher_comment.find()) {
						 String comment=matcher_comment.group();
						 if(comment != null){
							 comment=comment.replace("回复: ", "");
							 try {
								commentNum=Integer.parseInt(comment);
							} catch (NumberFormatException e) {
								commentNum=meta.getCommentNum();
							}
						 }
					}
					meta.setClickNum(clickNum);
					meta.setCommentNum(commentNum);
					return meta;
				}
			}
		}
		return null;
	}

	public static void main(String[] args) {
		String url="http://www.gzmama.com/thread-3818123-1-1.html";
		UrlMeta urlMeta=CrawlHTML.responseToURL(url);
		GzmamaNewsAnalyse a=new GzmamaNewsAnalyse();
		boolean detailPage=a.isDetailPage(url);
		if (detailPage) {
			NewsMeta parserHtml=a.parserHtml(urlMeta);
			System.out.println(parserHtml);
		} else {
			System.out.println("不符合正则");
		}

	}
	
	public boolean isNeedUpdate(){
		return true;
	}
}
